package cn.tj.controller;

import cn.tj.entity.Food;
import cn.tj.entity.Register;
import cn.tj.entity.Result;
import cn.tj.entity.User;
import cn.tj.service.UserService;
import cn.tj.util.JwtUtil;
import cn.tj.util.Md5Util;
import cn.tj.util.ThreadLocalUtil;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        //通过账号查找用户
        User findUser = userService.findByAccount(user.getAccount());

        //判断是否为空
        if(findUser==null)
            return Result.error("账号不存在");

        //判断密码是否一致
        if(Md5Util.checkPassword(user.getPassword(),findUser.getPassword())){
            //登录成功
            Map<String,Object> claims = new HashMap<>();
            claims.put("account",user.getAccount());
            claims.put("password",user.getPassword());

            String token = JwtUtil.genToken(claims);
            return Result.success(token);
        }else{
            return Result.error("账号密码不匹配");
        }
    }

    @PostMapping("/register")
    public Result register(@RequestBody User user){
        User byAccount = userService.findByAccount(user.getAccount());
        //验证账号是否被占用
        if(byAccount==null){ //查询为空，未被占用
            //查看注册请求库中是否存在
            Register register = userService.findRegisterByAccount(user.getAccount());
            if(register==null){
                userService.register(user);
                return Result.success("您的注册请求以提交，请耐心等待审核");
            }else{
                return Result.error("账号已被占用");
            }

        }else{
            return Result.error("账号已被占用");
        }
    }

    /**
     * 获取当前用户信息
     * @return
     */
    @GetMapping("/userInfo")
    public Result<User> userInfo(){
        User user = userService.userInfo();
        return Result.success(user);
    }

    /**
     * 编辑用户信息
     * @param user
     * @return
     */
    @PutMapping("/editInfo")
    public Result editInfo(@RequestBody User user){
        userService.editInfo(user);
        return Result.success();
    }

    /**
     * 更新密码
     * @param map
     * @return
     */
    @PatchMapping("editPassword")
    public Result editPassword(@RequestBody Map<String,String> map){
        String oldPwd = map.get("oldPwd");
        String password = map.get("checkPass");

        Map<String,Object> tMap = ThreadLocalUtil.get();
        String account =(String) tMap.get("account");
        //通过账号名查询
        User byAccount = userService.findByAccount(account);
        if(Md5Util.getMD5String(oldPwd).equals(byAccount.getPassword())){//判断输入的老密码与原密码是否一致
            userService.editPassword(byAccount.getAccount(),password);
            return Result.success();
        }else{
            return Result.error("原密码输入错误");
        }


    }

    /**
     * 更新头像
     * @param user
     * @return
     */
    @PatchMapping("/editAvatar")
    public Result editAvatar(@RequestBody User user){
        userService.editAvatar(user.getAvatar());
        return Result.success();
    }

}
